//
//  DetailViewController.m
//  HuiPinZhiJia
//
//  Created by MS on 16/8/30.
//  Copyright © 2016年 MS. All rights reserved.
//

#import "DetailViewController.h"
#import "SuperNav.h"
#import "detaioCell1.h"
#import "detailCell2.h"
#import "detailIntroduction.h"
#import "detailCommond.h"

@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSorce;
@end

@implementation DetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    
    self.tabBarController.tabBar.hidden = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"商品详情";
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.41 green:0.79 blue:1.00 alpha:1.00]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],UITextAttributeFont : [UIFont boldSystemFontOfSize:18]}];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTable];
    
}

- (void)createTable
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [ _tableView registerNib:[UINib nibWithNibName:@"detaioCell1" bundle:nil]forCellReuseIdentifier :@"detaioCell1"];
    [_tableView registerNib:[UINib nibWithNibName:@"detailCell2" bundle:nil] forCellReuseIdentifier:@"detailCell2"];
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth,screenWidth)];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, screenWidth - 10, screenWidth - 10)];
    imageView.image = [UIImage imageNamed:@"placeHolder.jpg"];
    [view addSubview:imageView];
    _tableView.tableHeaderView = view;
    
    [self.view addSubview:_tableView];
    
}

#pragma mark UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int a;
    switch (section)
    {
        case 0:
            return a=4;
            break;
         case 1:
            return a=1;
            break;
        case 2:
            return a=1;
        default:
            break;
    }
    return a;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
//            收藏部分
        static NSString * cellId = @"detaioCell1";
        detaioCell1 *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
            cell.CollectionBtn.tag = 1000;
            [cell.CollectionBtn addTarget:self action:@selector(cellBtn:) forControlEvents:UIControlEventTouchUpInside];
            return cell;
        }else if(indexPath.row == 1)
        {
//            购买数量
            static NSString *cellId = @"detailCell2";
            detailCell2 *cell1 = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
            return cell1;
        }else if(indexPath.row == 2)
        {
//            商品描述
        static NSString *CellIdentifier = @"Cell1";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.detailTextLabel.font = [UIFont systemFontOfSize:15];

        cell.detailTextLabel.text = @"商品描述";
        return cell;
        }else if(indexPath.row == 3)
        {
//            商品评论
            static NSString *CellIdentifier1 = @"Cell2";
            
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
            if (cell == nil)
            {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1];
            }
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.detailTextLabel.text = @"商品评论";
            cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(screenWidth*3/5,cell.frame.size.height*2/10, screenWidth/3, 30)];
            label.font = [UIFont systemFontOfSize:10];
            label.text = [NSString stringWithFormat:@"%d人评价 %d％好评",0,100];
            [cell addSubview:label];
            return cell;
        }
    }
    else if(indexPath.section == 1)
    {
//        立即购买
            static NSString *CellIdentifier2 = @"Cell3";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier2];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setBackgroundColor:[UIColor colorWithRed:0.33 green:0.67 blue:0.87 alpha:1.00]];
            btn.frame = CGRectMake(10, 5, screenWidth - 20, 70);
            [btn setTitle:@"立即购买" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(buyNow:) forControlEvents:UIControlEventTouchUpInside];
            [btn.layer setCornerRadius:5];
            [cell addSubview:btn];
        }
        return cell;
    }
    else if(indexPath.section == 2)
    {
//        相关商品
        static NSString *CellIdentifier2 = @"Cell3";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier2];
            cell.detailTextLabel.text = @"相关商品:";
            cell.detailTextLabel.font = [UIFont systemFontOfSize:20];
        }
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            return 150;
        }else if(indexPath.row == 1){
            return 80;
        }else{
            return 50;
        }
    }else if(indexPath.section == 1){
        return 75;
    }else{
        return 90;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 2)
        {
//            商品描述
            detailIntroduction *detail = [[detailIntroduction alloc]init];
            [self.navigationController pushViewController:detail animated:YES];
        }else if(indexPath.row == 3)
        {
//            商品评论
            UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            
            //由storyboard根据myView的storyBoardID来获取我们要切换的视图
            detailCommond *com = [story instantiateViewControllerWithIdentifier:@"detailCommond"];
            [self.navigationController pushViewController:com animated:YES];
        }
    }
}

- (void)cellBtn:(id)btn
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您确定收藏此商品么？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *ocAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:ocAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark 立即购买
- (void)buyNow:(UIButton *)btn
{
    UITabBarController* tab = [UIApplication sharedApplication].keyWindow.rootViewController;
    [self.navigationController popToRootViewControllerAnimated:NO];
    self.tabBarController.tabBar.hidden = NO;
    tab.selectedIndex = 3;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
